// Stubだよ！

#ifndef NDEBUG
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
#include <getopt.h> // for getopt
#include "COperation.hpp"
#include "CField.hpp"
#include "CCore.hpp"
#include "CPlayer.hpp"
#include "Utils.hpp"
#include "Datas.hpp"
#include "types.hpp"

using namespace std;

bool cmd_mode = false;

void interpret_args(int argc, char* argv[])
{
	int r;
	while((r = getopt(argc,argv,"od")) != -1)
	{
		switch(r)
		{
#		ifdef DEBUG
		case 'o':
			Datas::printall();
			break;
		case 'd':
			cmd_mode = true;
#		endif
		default:
			break;
		}
	}
}

static void printhelp()
{
	cout << "Dont use nonprintable key" << endl
		<< "oa attack : x y z damage" << endl
		<< "om move   : x y z" << endl
		<< "od dive" << endl
		<< "os surface" << endl
		<< "f show field" << endl
		<< "P show all player info" << endl
		<< "M show all mob info" << endl
		<< "p show current player info" << endl
		<< "m show current mob info" << endl
		<< "a add mob to player: type x y z" << endl
		<< "c clear operation from current mob" << endl
		<< "n change to next mob" << endl
		<< "r return to previous mob" << endl
		<< "h show help" << endl
		<< "q quit" << endl
		<< "Q quit from game";
}

template<class T, int n>
static bool validnum(T* t)
{
	return t->size() >= n;
}

/// processing ops from stdin
int process_ops(CCore& game)
{
	using namespace Operations;

	assert(game.Players.size() >= 2); // 2人以上
	for(uint i = 0; i < game.Players.size(); i++)
	{
player_loop:
		assert(game.Players[i]->Mobs.size() >= 1);
		for(uint j = 0; j < game.Players[i]->Mobs.size(); j++)
		{
mob_loop:
			int flow = 0; // 0:keep, 1:next 2:prev
			do
			{
				CMob& mob = *game.Players[i]->Mobs[j];
				
				// prompt
				cout << i + 1 << '-'
					<< j + 1 << " >" << flush;
				
				char in;
				cin >> in;
				switch(in)
				{
				case('o'):
					cin >> in;
					switch(in)
					{
					case('a'):
						{
							int x, y, z, d;
							cin >> x >> y >> z >> d;
							position_t p = {z, y ,z};
							mob.addOperation(new CAttack(p, d));
						}
						break;
					case('m'):
						{
							int x, y, z;
							cin >> x >> y >> z;
							position_t p = {x, y, z};
							mob.addOperation(new CMove(p));
						}
						break;
					case('s'):
						mob.addOperation(new CSurface);
						break;
					case('d'):
						mob.addOperation(new CDive);
						break;
					default:
						cout << "Unknown Operation Id." << endl;
						break;
					}
					break;
				case('f'):
					game.Field.print();
					break;
				case('p'):
					game.Players[i]->print();
					break;
				case('m'):
					mob.print();
					break;
				case('P'):
					game.Players.print();
					break;
				case('M'):
					game.Players.foreach<CMob>
						(std::mem_fun(&CMob::print));
					break;
				case('a'):
					{
						int t, x, y, z;
						cin >> t >> x >> y >> z;
						position_t p = {x, y, z};
						game.Players[i]->addMob(new CMob(t, p));
					}
					break;
				case('c'):
					cout << "Clear operations" << endl;
					while(mob.Ops.empty() != true){
						delete mob.Ops.back();
						mob.Ops.pop_back();
					}
					break;
				case('n'):
					cout << "Set to next mob" << endl;
					flow = 1;
					break;
				case('r'):
					cout << "Set to previous mob" << endl;
					flow = 2;
				case('q'):
					cout << "Quit" << endl;
					flow = -1;
					break;
				case('Q'):
					return -1;
				case('h'):
					cout << "Show help" << endl
						<< "Usage: COMMAND [ARGUMENTS...]" << endl
						<< "Example: m 1 2 3" << endl;
						printhelp();
					break;
				default: // invalid header
					cout << "Invalid header" << endl;
					break;
				}

				// skip to eol
				while(cin.get() != '\n');
				
				cout << "OK" << endl;
			} while(flow == 0);
			
			if(flow == 2) // decrease pointer
			{
				if(j == 0) // first mob
				{
					if(i != 0) // not first player
					{
						i--;
						j = game.Players[i]->Mobs.size() - 1;
						goto player_loop;
					}
				}else
				{
					j--;
					goto mob_loop;
				}
			}else if(flow == -1) //quit
			{
				goto quit;
			}
		}
	}
quit:
	return 0;
}

#endif

